﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using vmlModel.Entities;
using System.Windows.Controls;
using System.Windows;

namespace XAMLFactory
{
    public class FillBuilder
    {
        public Brush CreatevmlFill(vmlShapeBase shp)
        {
            if (shp.Fill == null) return new SolidColorBrush(shp.FillColor);
            if (!shp.Fill.LiveAtrrs.Contains("type") || shp.Fill.Type == "solid") return new SolidColorBrush(shp.FillColor);
            if (shp.Fill.Type.ToLower() == "gradient")
            {
                LinearGradientBrush lg = new LinearGradientBrush();//shp.Fill.Angle);  
                switch (shp.Fill.Focus)
                {
                    case 0:
                        lg.GradientStops.Add(new GradientStop(shp.Fill.Color, 0));
                        lg.GradientStops.Add(new GradientStop(shp.Fill.Color2, 1));
                        break;
                    case 100:
                        lg.GradientStops.Add(new GradientStop(shp.Fill.Color2, 0));
                        lg.GradientStops.Add(new GradientStop(shp.Fill.Color, 1));
                        break;
                    case -50:
                        lg.GradientStops.Add(new GradientStop(shp.Fill.Color, 0));
                        lg.GradientStops.Add(new GradientStop(shp.Fill.Color2, 0.5));
                        lg.GradientStops.Add(new GradientStop(shp.Fill.Color, 1));
                        break;
                    case 50:
                        lg.GradientStops.Add(new GradientStop(shp.Fill.Color2, 0));
                        lg.GradientStops.Add(new GradientStop(shp.Fill.Color, 0.5));
                        lg.GradientStops.Add(new GradientStop(shp.Fill.Color2, 1));
                        break;

                    default:
                        break;
                }
                
                switch (shp.Fill.Angle)
                {
                    case 0:
                        lg.StartPoint = new Point(0.5, 1);
                        lg.EndPoint = new Point(0.5, 0);
                        break;
                    case 45:
                        lg.StartPoint = new Point(0, 1);
                        lg.EndPoint = new Point(1, 0);
                        break;
                    case 90:
                        lg.StartPoint = new Point(0, 0.5);
                        lg.EndPoint = new Point(1, 0.5);
                        break;
                    case 180:
                        lg.StartPoint = new Point(0.5, 0);
                        lg.EndPoint = new Point(0.5, 1);
                        break;
                    case 225:
                        lg.StartPoint = new Point(1, 1);
                        lg.EndPoint = new Point(0, 0);
                        break;                    
                    case 270:
                        lg.StartPoint = new Point(1, 0.5);
                        lg.EndPoint = new Point(0, 0.5);
                        break;
                    case 315:
                        lg.StartPoint = new Point(1, 0);
                        lg.EndPoint = new Point(0, 1);
                        break;


                    default:
                        break;
                }
                return lg;

            }
            //RadialGradientBrush 
            if (shp.Fill.Type.ToLower() == "gradientradial")
            {
                RadialGradientBrush rg = new RadialGradientBrush();//shp.Fill.Angle);  
                switch (shp.Fill.Focus)
                {
                    case 0:
                        rg.GradientStops.Add(new GradientStop(shp.Fill.Color, 0));
                        rg.GradientStops.Add(new GradientStop(shp.Fill.Color2, 1));
                        break;
                    case 100:
                        rg.GradientStops.Add(new GradientStop(shp.Fill.Color2, 0));
                        rg.GradientStops.Add(new GradientStop(shp.Fill.Color, 1));
                        break;
                    case -50:
                        rg.GradientStops.Add(new GradientStop(shp.Fill.Color, 0));
                        rg.GradientStops.Add(new GradientStop(shp.Fill.Color2, 0.5));
                        rg.GradientStops.Add(new GradientStop(shp.Fill.Color, 1));
                        break;
                    case 50:
                        rg.GradientStops.Add(new GradientStop(shp.Fill.Color2, 0));
                        rg.GradientStops.Add(new GradientStop(shp.Fill.Color, 0.5));
                        rg.GradientStops.Add(new GradientStop(shp.Fill.Color2, 1));
                        break;

                    default:
                        break;
                }

                switch (shp.Fill.Angle)
                {
                    case 0:
                        rg.GradientOrigin = new Point(0.5, 0.5);
                        break;
                    case 45:
                        rg.GradientOrigin = new Point(0.5, 0.5);
                        break;
                    case 90:
                        rg.GradientOrigin = new Point(0.5, 0.5);
                        break;
                    case 180:
                        rg.GradientOrigin = new Point(0.5, 0.5);
                        break;
                    case 225:
                        rg.GradientOrigin = new Point(0.5, 0.5);
                        break;
                    case 270:
                        rg.GradientOrigin = new Point(0.5, 0.5);
                        break;
                    case 315:
                        rg.GradientOrigin = new Point(0.5, 0.5);
                        break;


                    default:
                        break;
                }
                return rg;
            }
            return new SolidColorBrush(shp.FillColor);
        }
    }
}
